% Run this script to generate speckle patterns
clc;
close all;
clear;
%% Image parameters
% Image size
image_row = 100;
image_col = 100;

% Expected minimum speckle distance
EMSD = 5;

% Speckle radius
radius = 2;

% Step size of iteration
step = 0.1;

% After the number of failed random generation of speckle coordinates
% reaches this value, iteration begins to fill the remaining gaps;
failure_limit = 20;
%% Main program
% Initialization
centers = zeros(50000, 2);  % Stores the coordinates of the speckle centers
occupied_map = false(image_row, image_col);  % Marks occupied pixels
available_map = true(image_row, image_col);  % Marks available pixels
[X, Y] = meshgrid(1:image_col, 1:image_row);  % Generate coordinate grid

% Step 1: Generate random speckle centers
[centers, counter, occupied_map, available_map] = generate_random_speckles(centers, occupied_map, available_map, X, Y, EMSD, failure_limit);

% Step 2: Fill remaining space
[centers, counter] = fill_remaining_space(centers, counter, occupied_map, available_map, X, Y, EMSD, step);

% Coordinates result
centers_f = centers(1:counter, :);

% Draw speckle pattern
I = draw_speckle_pattern(image_row, image_col, centers_f, radius);
%% Visualization
figure
imshow(I)